package com.daocao.hua.fragment;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.allen.library.SuperButton;
import com.daocao.hua.R;
import com.daocao.hua.activity.Application;
import com.daocao.hua.activity.EarnMoneyActivity;
import com.daocao.hua.activity.MainActivityNew;
import com.daocao.hua.activity.MyQrCodeActivity;
import com.daocao.hua.activity.PermissionActivity;
import com.daocao.hua.activity.WechatChatActivity;
import com.daocao.hua.adapter.OverDueBottomAdapter;
import com.daocao.hua.api.Api;
import com.daocao.hua.constant.Const;
import com.daocao.hua.constant.UserGlobal;
import com.daocao.hua.event.ChangeHomePagerEvent;
import com.daocao.hua.event.HomeBtnOneEvent;
import com.daocao.hua.event.HomeBtnTouchEvent;
import com.daocao.hua.event.SelectMoneyEvent;
import com.daocao.hua.model.V3HomeIndexModel;
import com.daocao.hua.util.DisplayUtil;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.Spans;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.Dialog;
import com.daocao.hua.view.ObservableScrollView;
import com.lljjcoder.view.ViewHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-6-11.
 */

public class FragmentV3Home extends BaseFragment implements ObservableScrollView.ScrollViewListener {
    @BindView(R.id.txt_address)
    TextView txtAddress;
    @BindView(R.id.btn_chat)
    TextView btnChat;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.btn_download)
    SuperButton btnDownload;
    @BindView(R.id.pager_one)
    ViewPager pagerOne;
    //    @BindView(R.id.indicator_one)
//    LinePageIndicator indicatorOne;
//    @BindView(R.id.pager_two)
//    ViewPager pagerTwo;
//    @BindView(R.id.indicator_two)
//    LinePageIndicator indicatorTwo;
    Unbinder unbinder;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.txt_notice)
    ViewFlipper txtNotice;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    @BindView(R.id.txt_loan_tips)
    TextView txtLoanTips;
    @BindView(R.id.layout_loan_tips)
    LinearLayout layoutLoanTips;
    @BindView(R.id.txt_yuqi_sign)
    TextView txtYuqiSign;
    @BindView(R.id.txt_type_tips)
    TextView txtTypeTips;
    @BindView(R.id.list_bottom_one)
    RecyclerView listBottomOne;
    @BindView(R.id.btn_home_btn_one)
    ImageView btnHomeBtnOne;
    @BindView(R.id.btn_home_btn_two)
    ImageView btnHomeBtnTwo;
    @BindView(R.id.scrollView)
    ObservableScrollView scrollView;
    private boolean isGetData = false;
    public String loanType = "1";
    private int currentPage = 0;
    private mTouchListener touchListener;
    private OverDueBottomAdapter overDueBottomAdapter;
    private int totalWidth;
    private int totalHeight;
    private static final int TIME = 5 * 1000;  //验证码倒计时时间
    private CountDownTimer timer;
    private View.OnTouchListener onTouchListenerTwo = new View.OnTouchListener() {
        private int lastX;
        private int lastY;
        int firstX, firstY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // 获取view相对于手机屏幕的xy值
            int x = (int) event.getRawX();
            int y = (int) event.getRawY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    EventBus.getDefault().post(new HomeBtnTouchEvent(0));
                    firstX = (int) event.getRawX();
                    firstY = (int) event.getRawY();

                    refresh.setEnabled(false);
                    break;
                case MotionEvent.ACTION_MOVE:
                    int deltaX = x - lastX;
                    int deltaY = y - lastY;
                    int translationX = (int) (ViewHelper.getTranslationX(btnHomeBtnTwo) + deltaX);
                    int translationY = (int) (ViewHelper.getTranslationY(btnHomeBtnTwo) + deltaY);

                    ViewHelper.setTranslationX(btnHomeBtnTwo, translationX);

                    if (translationY < 0 && Math.abs(translationY) > btnHomeBtnTwo.getTop()) {

                    } else {
                        if (translationY <= totalHeight - btnHomeBtnTwo.getBottom() - btnHomeBtnTwo.getHeight() / 2) {
                            ViewHelper.setTranslationY(btnHomeBtnTwo, translationY);
                        }
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    getTimer().start();
                    refresh.setEnabled(true);
                    if (((firstX - 5) <= (int) event.getRawX() && (int) event.getRawX() <= (firstX + 5))
                            && ((int) event.getRawY() >= (firstY - 5) && (int) event.getRawY() <= (firstY + 5))) {
                        EventBus.getDefault().post(new HomeBtnOneEvent(1));
                    }
                    if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnTwo)) >= totalWidth / 2 - btnHomeBtnOne.getWidth() / 2) {
                        ViewHelper.setTranslationX(btnHomeBtnTwo, -(totalWidth) + (btnHomeBtnTwo.getWidth()));
                    } else {
                        ViewHelper.setTranslationX(btnHomeBtnTwo, 0);
                    }
                    break;
                default:
                    break;
            }
            lastX = x;
            lastY = y;
            return true;
        }
    };

    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        private int lastX;
        private int lastY;
        int firstX, firstY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // 获取view相对于手机屏幕的xy值
            int x = (int) event.getRawX();
            int y = (int) event.getRawY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    EventBus.getDefault().post(new HomeBtnTouchEvent(1));
                    firstX = (int) event.getRawX();
                    firstY = (int) event.getRawY();

                    refresh.setEnabled(false);
                    break;
                case MotionEvent.ACTION_MOVE:
                    int deltaX = x - lastX;
                    int deltaY = y - lastY;
                    int translationX = (int) (ViewHelper.getTranslationX(btnHomeBtnOne) + deltaX);
                    int translationY = (int) (ViewHelper.getTranslationY(btnHomeBtnOne) + deltaY);

                    ViewHelper.setTranslationX(btnHomeBtnOne, translationX);

                    if (translationY < 0 && Math.abs(translationY) > btnHomeBtnOne.getTop()) {

                    } else {
                        if (translationY <= totalHeight - btnHomeBtnOne.getBottom() - btnHomeBtnOne.getHeight() / 2) {
                            ViewHelper.setTranslationY(btnHomeBtnOne, translationY);
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    getTimer().start();
                    refresh.setEnabled(true);
                    if (((firstX - 5) <= (int) event.getRawX() && (int) event.getRawX() <= (firstX + 5))
                            && ((int) event.getRawY() >= (firstY - 5) && (int) event.getRawY() <= (firstY + 5))) {
                        EventBus.getDefault().post(new HomeBtnOneEvent(0));
                    }
                    if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnOne)) >= totalWidth / 2 - btnHomeBtnOne.getWidth() / 2) {
                        ViewHelper.setTranslationX(btnHomeBtnOne, -(totalWidth) + (btnHomeBtnOne.getWidth()));
                    } else {
                        ViewHelper.setTranslationX(btnHomeBtnOne, 0);
                    }
                    break;
                default:
                    break;
            }
            lastX = x;
            lastY = y;
            return true;
        }
    };
    private V3HomeIndexModel.DataBean.DealStatusBean totalData;


    @Override
    protected int setLayoutId() {
        return R.layout.fragment_v3_home;
    }

    @Override
    protected void initView() {
        totalWidth = Util.getDisplayMetrics(getContext()).widthPixels;
        totalHeight = Util.getDisplayMetrics(getContext()).heightPixels;
        scrollView.setScrollViewListener(this);
        pagerOne.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
                if (position == pagerOne.getChildCount() - 1) {
                    if (touchListener == null) {
                        touchListener = new mTouchListener();
                    }
                    pagerOne.setOnTouchListener(touchListener);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getIndex();
            }
        });
        txtAddress.setText(PreferenceUtils.getString(getContext(), Const.KEY.LOCATION_ADDRESS, ""));
        overDueBottomAdapter = new OverDueBottomAdapter(getContext());
        listBottomOne.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        listBottomOne.setAdapter(overDueBottomAdapter);

        btnHomeBtnOne.setOnTouchListener(onTouchListener);
        btnHomeBtnTwo.setOnTouchListener(onTouchListenerTwo);
        EventBus.getDefault().register(this);


    }

    private CountDownTimer getTimer() {
        if (timer != null) {
            return timer;
        }
        timer = new CountDownTimer(TIME, 1000) {

            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onTick(long millisUntilFinished) {
                try {

                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinish() {
                if (ViewHelper.getTranslationX(btnHomeBtnTwo) == 0 || ViewHelper.getTranslationX(btnHomeBtnTwo) == -(totalWidth) + (btnHomeBtnTwo.getWidth())) {
                    if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnTwo)) >= totalWidth / 2 - btnHomeBtnTwo.getWidth() / 2) {
                        ViewHelper.setTranslationX(btnHomeBtnTwo, -(totalWidth) + (btnHomeBtnTwo.getWidth() / 2));
                    } else {
                        ViewHelper.setTranslationX(btnHomeBtnTwo, btnHomeBtnTwo.getWidth() / 2);
                    }

                    if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnOne)) >= totalWidth / 2 - btnHomeBtnOne.getWidth() / 2) {
                        ViewHelper.setTranslationX(btnHomeBtnOne, -(totalWidth) + (btnHomeBtnOne.getWidth()));
                    } else {
                        ViewHelper.setTranslationX(btnHomeBtnOne, 0);
                    }
                } else {

                    if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnOne)) >= totalWidth / 2 - btnHomeBtnOne.getWidth() / 2) {
                        ViewHelper.setTranslationX(btnHomeBtnOne, -(totalWidth) + (btnHomeBtnOne.getWidth() / 2));
                    } else {
                        ViewHelper.setTranslationX(btnHomeBtnOne, btnHomeBtnOne.getWidth() / 2);
                    }

                    if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnTwo)) >= totalWidth / 2 - btnHomeBtnTwo.getWidth() / 2) {
                        ViewHelper.setTranslationX(btnHomeBtnTwo, -(totalWidth) + (btnHomeBtnTwo.getWidth()));
                    } else {
                        ViewHelper.setTranslationX(btnHomeBtnTwo, 0);
                    }
                }
                timer.start();
            }
        };
        return timer;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClickFloat(HomeBtnOneEvent msg) {
//        悬浮按钮的点击事件
        if (msg.type == 0) {
            if (!Util.checkLogin(getActivity())) {
                return;
            }
            EventBus.getDefault().post(new ChangeHomePagerEvent(2));
        } else if (msg.type == 1) {
            if (!Util.checkLogin(getActivity())) {
                return;
            }
            EventBus.getDefault().post(new ChangeHomePagerEvent(3));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClickTouch(HomeBtnTouchEvent msg) {
        if (msg.type == 0) {

            if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnOne)) >= totalWidth / 2 - btnHomeBtnOne.getWidth() / 2) {
                ViewHelper.setTranslationX(btnHomeBtnOne, -(totalWidth) + (btnHomeBtnOne.getWidth() / 2));
            } else {
                ViewHelper.setTranslationX(btnHomeBtnOne, btnHomeBtnOne.getWidth() / 2);
            }
        } else if (msg.type == 1) {
            if (Math.abs(ViewHelper.getTranslationX(btnHomeBtnTwo)) >= totalWidth / 2 - btnHomeBtnOne.getWidth() / 2) {
                ViewHelper.setTranslationX(btnHomeBtnTwo, -(totalWidth) + (btnHomeBtnTwo.getWidth() / 2));
            } else {
                ViewHelper.setTranslationX(btnHomeBtnTwo, btnHomeBtnTwo.getWidth() / 2);
            }
        }
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (refresh != null) {
            refresh.setEnabled(scrollView.getScrollY() == 0);
        }

    }

    private class mTouchListener implements View.OnTouchListener {

        float x1 = 0;
        float x2 = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (currentPage == 2) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //当手指按下的时候
                        x1 = event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        //当手指离开的时候
                        x2 = event.getX();
                        if (x1 - x2 > 50) {
                            if (pagerOne.getChildCount() != 0) {
                                pagerOne.setCurrentItem(0);
                            }

                        }
                        break;
                }
            }
            return false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getTimer().start();
        getIndex();
    }

    public void setAddress(String text) {
        if (txtAddress != null)
            txtAddress.setText(text);
    }


    public void getIndex() {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.makeText(getContext(), "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getV3HomeIndex().compose(RxLifeUtil.checkOn(getContext())).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    setData(result.data);

                });
    }


    public class PagerOneAdapter extends FragmentPagerAdapter {

        //        String[] tabs = {"未还账单", "还款记录"};
        private List<Fragment> fragments = new ArrayList<>();

        public PagerOneAdapter(FragmentManager fm, List<V3HomeIndexModel.DataBean.DealUserListBean> item) {
            super(fm);
            fragments.clear();
            for (int i = 0; i < item.size(); i++) {
                V3HomeBottomOneFragment fragment = new V3HomeBottomOneFragment();
                fragment.setData(item.get(i));
                fragments.add(fragment);
            }
        }


        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {

            return fragments.get(position);
        }
    }


    private void setData(V3HomeIndexModel.DataBean data) {

        //0：失败、1：走新申请流程、2：继续上次未完成的流程、3：申请审核中、4：申请未通过、5：等待放款、6：还款中
        totalData = data.deal_status;
        if (data.deal_status != null) {

            UserGlobal.setDealId(data.deal_status.deal_id);
            if (!TextUtils.isEmpty(data.deal_status.color)) {
                btnDownload.setShapeSolidColor(Color.parseColor(data.deal_status.color));
                btnDownload.setUseShape();
            }
            StringBuilder builder = new StringBuilder();
            if (data.deal_status.remark_text != null && data.deal_status.remark_text.size() != 0) {

                for (int i = 0; i < data.deal_status.remark_text.size(); i++) {
                    builder.append(data.deal_status.remark_text.get(i) + "\n");
                }

            }

            txtYuqiSign.setVisibility(View.GONE);
            if (TextUtils.isEmpty(data.deal_status.status)) {
                loanType = "0";
            } else {
                loanType = data.deal_status.status;
            }

            switch (loanType) {
                case "0":
                    txtMoney.setText(data.deal_status.money);
                    txtMoney.setTextColor(0xffB6BCDB);
                    btnDownload.setText("立即下款");
                    txtTips.setText("申请借款金额(元)");
                    layoutLoanTips.setVisibility(View.GONE);
                    txtLoanTips.setVisibility(View.VISIBLE);
                    txtLoanTips.setText(builder.toString());
                    break;
                case "1":
                    txtMoney.setText(data.user.deal_min_quota + "~" + data.user.deal_max_quota);
                    txtMoney.setTextColor(0xffFE5A59);
                    btnDownload.setText("立即下款");
                    txtTips.setText("可借额度范围(元)");
                    layoutLoanTips.setVisibility(View.VISIBLE);
                    txtLoanTips.setVisibility(View.GONE);
                    txtLoanTips.setText(builder.toString());
                    break;
                case "2":
                    txtMoney.setText(data.user.deal_min_quota + "~" + data.user.deal_max_quota);
                    txtMoney.setTextColor(0xffFE5A59);
                    btnDownload.setText("立即下款");
                    txtTips.setText("可借额度范围(元)");

                    layoutLoanTips.setVisibility(View.VISIBLE);
                    txtLoanTips.setVisibility(View.GONE);
                    txtLoanTips.setText(builder.toString());
                    break;
                case "3":
                    txtMoney.setText(data.deal_status.money);
                    txtMoney.setTextColor(0xffFF7A31);
                    btnDownload.setText("审核中");
                    txtTips.setText("已申请借款金额(元)");
                    layoutLoanTips.setVisibility(View.GONE);
                    txtLoanTips.setVisibility(View.VISIBLE);

                    Spanned text = Spans.with(this).font(builder.substring(0, builder.length() - 2), getResources().getDimensionPixelSize(R.dimen.x30), 0xffB6BCDB).
                            font("点击认证\n", getResources().getDimensionPixelSize(R.dimen.x30), 0xff4C81FF).
                            build();
                    txtLoanTips.setText(text);
                    txtLoanTips.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            EventBus.getDefault().post(new ChangeHomePagerEvent(1));
                        }
                    });
                    break;
                case "4":
                    txtMoney.setText(data.deal_status.money);
                    txtMoney.setTextColor(0xffB6BCDB);

                    btnDownload.setText("审核失败");
                    txtTips.setText("申请借款金额(元)");
                    layoutLoanTips.setVisibility(View.GONE);
                    txtLoanTips.setVisibility(View.VISIBLE);
                    txtLoanTips.setText(builder.toString());
                    if (UserGlobal.isShowDealDialog(data.deal_status.deal_id)) {
                        Dialog.showCheckFail(getContext());
                    }

                    break;
                case "5":
                    txtMoney.setText(data.deal_status.money);
                    txtMoney.setTextColor(0xffFFC231);

                    btnDownload.setText("放款中");
                    txtTips.setText("已申请借款金额(元)");
                    layoutLoanTips.setVisibility(View.GONE);
                    final SpannableStringBuilder style = new SpannableStringBuilder();
                    if (builder.length() != 0) {
                        style.append(builder.substring(0, builder.length() - 2) + "  发帖赚钱  邀请赚钱");
                    } else {
                        style.append("发帖赚钱  邀请赚钱");
                    }


                    //设置部分文字点击事件
                    ClickableSpan clickableSpan = new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
//                            Toast.makeText(MainActivity.this, "触发点击事件!", Toast.LENGTH_SHORT).show();
                            Util.startActivity(getContext(), EarnMoneyActivity.class);
                        }

                        @Override
                        public void updateDrawState(TextPaint ds) {
                            ds.setUnderlineText(false);
                        }
                    };

                    ClickableSpan clickableSpan1 = new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            Util.startActivity(getContext(), MyQrCodeActivity.class);
//                            Toast.makeText(MainActivity.this, "触发点击事件!", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void updateDrawState(TextPaint ds) {
                            ds.setUnderlineText(false);
                        }
                    };
                    style.setSpan(clickableSpan, style.toString().indexOf("发帖赚钱"), style.toString().indexOf("发帖赚钱") + 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                    style.setSpan(clickableSpan1, style.toString().indexOf("邀请赚钱"), style.toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//                    txtLoanTips.setText(style);

                    //设置部分文字颜色
                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#4C81FF"));
                    style.setSpan(foregroundColorSpan, style.toString().indexOf("发帖赚钱"), style.toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


                    //配置给TextView
                    txtLoanTips.setMovementMethod(LinkMovementMethod.getInstance());
                    txtLoanTips.setText(style);
//                    Spanned text1 = Spans.with(this).font(builder.substring(0, builder.length() - 2), getResources().getDimensionPixelSize(R.dimen.x30), 0xffB6BCDB).
//                            font("  发帖赚钱  邀请赚钱\n", getResources().getDimensionPixelSize(R.dimen.x30), 0xff4C81FF).
//                            build();
//
//
//                    txtLoanTips.setText(text1);
                    txtLoanTips.setVisibility(View.VISIBLE);
//                    txtLoanTips.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Util.startActivity(getContext(), EarnMoneyActivity.class);
//                        }
//                    });
                    break;
                case "6":
                    btnDownload.setText("立即还款");
                    txtTips.setText("近期待还金额(元)");
                    layoutLoanTips.setVisibility(View.GONE);
                    txtLoanTips.setVisibility(View.VISIBLE);
                    txtMoney.setText(data.deal_status.money);
                    txtLoanTips.setText(builder.toString());
                    if (data.deal_status.overdue_days > 0) {
                        txtMoney.setTextColor(0xffFE5A59);
                        txtYuqiSign.setVisibility(View.VISIBLE);
                        txtYuqiSign.setText("逾期\n" + data.deal_status.overdue_days + "天");
                    } else {
                        txtMoney.setTextColor(0xff3BCF8F);
                    }
                    break;
            }

//        if (isGetData) {
//            return;
//        }
            if (loanType.equals("6") && data.deal_status.overdue_days > 0) {
                listBottomOne.setVisibility(View.VISIBLE);
                pagerOne.setVisibility(View.GONE);
                txtTypeTips.setText("逾期公示");
//                pagerOne.setPageMargin(DisplayUtil.dip2px(getContext(), 15));
//                pagerOne.setAdapter(new PagerOneAdapter(getChildFragmentManager(), data.overdue_notice, 1));
                overDueBottomAdapter.update(data.overdue_notice);
//                indicatorOne.setVisibility(View.GONE);
            } else {
                listBottomOne.setVisibility(View.GONE);
                pagerOne.setVisibility(View.VISIBLE);
                txtTypeTips.setText("借款人说");
                pagerOne.setPageMargin(DisplayUtil.dip2px(getContext(), 15));
                pagerOne.setAdapter(new PagerOneAdapter(getChildFragmentManager(), data.deal_user_list));
//                indicatorOne.setVisibility(View.VISIBLE);
//                indicatorOne.setViewPager(pagerOne);
            }

        }
//        if (data.banners != null && data.banners.size() != 0) {
//            pagerTwo.setAdapter(new PagerTwoAdapter(getChildFragmentManager(), data.banners));
//            indicatorTwo.setViewPager(pagerTwo);
//        }


        if (data.notice != null && data.notice.size() != 0 && !isGetData) {
            if (txtNotice.getChildCount() != 0) {
                txtNotice.removeAllViews();
            }
            for (int i = 0; i < data.notice.size(); i++) {
                LinearLayout layout = (LinearLayout) View.inflate(getContext(), R.layout.item_home_notice, null);
                TextView txtLeft = (TextView) layout.findViewById(R.id.txt_left);
                txtLeft.setText(data.notice.get(i).text);
                TextView txtRight = (TextView) layout.findViewById(R.id.txt_right);
                txtRight.setText(data.notice.get(i).time);
                txtNotice.addView(layout);
            }
        }

        isGetData = true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        unbinder.unbind();
    }

    @OnClick({R.id.btn_chat, R.id.btn_download, R.id.txt_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_chat:
//                Util.startActivity(getActivity(), SelectMoneyV3Activity.class);
                Util.startActivity(getActivity(), WechatChatActivity.class);
                break;
            case R.id.btn_download:
                if (!Util.checkLogin(getActivity())) {
                    return;
                }

                //0：失败、1：走新申请流程、2：继续上次未完成的流程、3：申请审核中、4：申请未通过、5：等待放款、6：还款中
                if (TextUtils.isEmpty(loanType)) {
                    getIndex();
                    return;
                }
                if (PreferenceUtils.getBoolean(getContext(), "is_locat", false)) {
                    switch (loanType) {
                        case "0":
                            break;
                        case "1":
                            if(totalData.download!=null&&!TextUtils.isEmpty(totalData.download.android)){
                                ((MainActivityNew) getActivity()).createDownloadManager(totalData.download.android);
                            }else {
                                EventBus.getDefault().post(new SelectMoneyEvent());
                            }
                            break;
                        case "2":
                            if(totalData.download!=null&&!TextUtils.isEmpty(totalData.download.android)){
                                ((MainActivityNew) getActivity()).createDownloadManager(totalData.download.android);
                            }else {
                                EventBus.getDefault().post(new SelectMoneyEvent());
                            }
                            break;
                        case "3":
                            break;
                        case "4":
                            Dialog.showCheckFail(getContext());
                            break;
                        case "5":
                            break;
                        case "6":
                            ((MainActivityNew) getActivity()).getRepayInfo();
                            break;
                    }


                } else {
                    checkPermission(new PermissionActivity.CheckPermListener() {
                                        @Override
                                        public void superPermission() {
                                            PreferenceUtils.putBoolean(Application.getInstance(), "is_locat", true);
                                            ((MainActivityNew) getActivity()).initLocation();
                                        }
                                    }, R.string.location_p, R.string.get_location,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.READ_PHONE_STATE);
                }
                break;

            case R.id.txt_address:
                checkPermission(new PermissionActivity.CheckPermListener() {
                                    @Override
                                    public void superPermission() {
                                        PreferenceUtils.putBoolean(Application.getInstance(), "is_locat", true);
                                        ((MainActivityNew) getActivity()).initLocation();
                                    }
                                }, R.string.location_p, R.string.get_location,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.READ_PHONE_STATE);
                break;
        }
    }
}
